package com.example.Android_test.util;

import android.util.Log;
import com.example.Android_test.activity.Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 31.05.13
 * Time: 14:00
 * To change this template use File | Settings | File Templates.
 */
public class SimpleTimer {
    private boolean isRunning = false;
    private List<ActionListener> stepListeners = new ArrayList<ActionListener>();
    private Timer timer;

    public SimpleTimer() {
        timer = new Timer();
        timer.schedule(task, 1000, 1000);

    }


    private TimerTask task = new TimerTask() {
        @Override
        public void run() {
            if (isRunning) stepFireEvent();
        }
    };

    public void deleteTimer() {
        timer.cancel();
    }

    public void startTimer() {
        isRunning = true;
    }

    public void stopTimer() {
        isRunning = false;
    }

    public void addStepListener(ActionListener listener) {
        stepListeners.add(listener);
    }

    public void removeStepListener(ActionListener listener) {
        stepListeners.remove(listener);
    }

    private void stepFireEvent() {
        for (ActionListener listener : stepListeners) {
            listener.actionPerformed(null);
        }
    }

    public interface ActionListener {
        public void actionPerformed(Object o);
    }
}
